<?php
/*
Module Group: text
Module Group Name: 文本
Description: 这是一个用来处理文本信息的插件
Version: 1.0
Display: 0
Iconurl: http://www.happyidea.com.cn/resource/wx_icon/text.png
Author: snaill
Modules: text
*/
//ihziluoh添加多客服
class transferCustomerServiceModule extends Module {	
	/*
	 * 激活创建数据库
	*/
	public function activate() {
		Module::importSQL(module_sql_path('transferCustomerService'));
	}
	
	
	
	public function run($message, $material, $userStatus) {
		//echo 'asdfasdf';
		Log::write('Enter function transferCustomerServiceModule');		
		$materialObj = simplexml_load_string($material->xml,
				'SimpleXMLElement', LIBXML_NOCDATA);
		$now=strtotime(date('Y-m-d H:i:s',time()));
		$start=date('Y-m-d',time()).' '.$materialObj->starttime;
		$end=date('Y-m-d',time()).' '.$materialObj->endtime;
		$start=strtotime($start);
		$end=strtotime($end);
		//非人工客服时间提示
		$notservicemsg=$materialObj->notservicemsg;
		//成功转入人工客服提示
		$servicesuccessmsg=$materialObj->servicesuccessmsg;
		if($start>$now or $now>$end){
			$rm = $message->reply(message_type_text);
			$rm->content = (String)$notservicemsg;
		}else{
			//插入接入多客服记录 start
			$userInfo = WeixinApi::getUserInfo($message->fromUserName);
			if (!isset($userInfo)) {
				return;
			}
			 
			$openid=$message->fromUserName;
			$model = M("module_service_accesstime");
			$modelUserInfo = M("module_service_userinfo");
			//访问时间表插入记录
			$model->create();
			$model->openid = $openid;
			$model->accesstime = date('Y-m-d H:i:s',time());
			$model->add();
			//用户表插入记录
			$arr=$modelUserInfo->where("openid='$openid'")->find();
			if(!isset($arr)){
				$modelUserInfo->create();
				$modelUserInfo->openid = $openid;
				$modelUserInfo->subscribe = $userInfo->subscribe;
				$modelUserInfo->nickname = $userInfo->nickname;
				$modelUserInfo->sex =$userInfo->sex;
				$modelUserInfo->language = $userInfo->language;
				$modelUserInfo->city = $userInfo->city;
				$modelUserInfo->province = $userInfo->province;
				$modelUserInfo->country = $userInfo->country;
				$modelUserInfo->headimgurl = $userInfo->headimgurl;
				$modelUserInfo->subscribe_time =$userInfo->subscribe_time;
				$modelUserInfo->add();
			}
			//插入接入多客服记录 end
			$rm = $message->reply(message_type_service);	
			WeixinApi::sendText($message->fromUserName,$servicesuccessmsg);
		}
		return $rm;
	}
}

$module = new transferCustomerServiceModule();
Module::addModule($module);